package com.ruoyi.student.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.student.domain.SysStudent;
import com.ruoyi.student.mapper.SysStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.SysClassMapper;
import com.ruoyi.student.domain.SysClass;
import com.ruoyi.student.service.ISysClassService;

/**
 * 班级Service业务层处理
 *
 * @author Xsw
 * @date 2024-03-22
 */
@Service
public class SysClassServiceImpl implements ISysClassService {
    @Autowired
    private SysClassMapper sysClassMapper;
    @Autowired
    private SysStudentMapper sysStudentMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询班级
     *
     * @param id 班级主键
     * @return 班级
     */
    @Override
    public SysClass selectSysClassById(Long id) {
        return sysClassMapper.selectSysClassById(id);
    }

    /**
     * 查询班级列表
     *
     * @param sysClass 班级
     * @return 班级
     */
    @Override
    public List<SysClass> selectSysClassList(SysClass sysClass) {
        redisTemplate.delete("classList");
        List<SysClass> sysClassList = sysClassMapper.selectSysClassList(sysClass);
        // 将班级列表保存到redis中
        redisService.setCacheList("classList", sysClassList);
        return sysClassList;
    }


    /**
     * 新增班级
     *
     * @param sysClass 班级
     * @return 结果
     */
    @Override
    public int insertSysClass(SysClass sysClass) {
        return sysClassMapper.insertSysClass(sysClass);
    }

    /**
     * 修改班级
     *
     * @param sysClass 班级
     * @return 结果
     */
    @Override
    public int updateSysClass(SysClass sysClass) {
        return sysClassMapper.updateSysClass(sysClass);
    }

    /**
     * 批量删除班级
     *
     * @param ids 需要删除的班级主键
     * @return 结果
     */
    @Override
    public int deleteSysClassByIds(Long[] ids) {
        return sysClassMapper.deleteSysClassByIds(ids);
    }

    /**
     * 删除班级信息
     *
     * @param id 班级主键
     * @return 结果
     */
    @Override
    public int deleteSysClassById(Long id) {
        return sysClassMapper.deleteSysClassById(id);
    }

    @Override
    public void importClass(List<SysClass> classes) {
        sysClassMapper.batchInsert(classes);
    }

    @Override
    public List<SysClass> classAndStudents() {
        List<SysClass> sysClassList = sysClassMapper.selectSysClassList(new SysClass());
        Map<Long, List<SysStudent>> classMap = sysClassList.stream()
                .collect(Collectors.toMap(SysClass::getId, SysClass::getStudentList));
        List<SysStudent> sysStudents = sysStudentMapper.selectSysStudentList(new SysStudent());
        sysStudents.forEach(ss -> {
            Optional.ofNullable(classMap.get(ss.getClassId())).ifPresent(list -> {
                list.add(ss);
            });
        });
        return sysClassList;
    }

    @Override
    public List<SysClass> getClassByCampus(String campus) {
        return sysClassMapper.getClassByCampus(campus);
    }
}
